<?php
/**
 * Zend Framework Initializer plugin.
 *
 * @category Smithy
 * @package Smithy_Plugin
 * @author Tamás Kovács
 * @version 1.0
 */


/**
 *
 * Initializes configuration depndeing on the type of environment
 * (test, development, production, etc.)
 *
 * This can be used to configure environment variables, databases,
 * layouts, routers, helpers and more
 *
 */
class Smithy_Plugin_Initialize extends Zend_Controller_Plugin_Abstract
{
    /**
     * @var Zend_Config
     */
    protected static $_config;

    /**
     * @var Zend_Log
     */
    protected static $_log;

    /**
     * @var string Current environment
     */
    protected $_env;

    /**
     * @var Zend_Controller_Front
     */
    protected $_front;

    /**
     * @var string Path to application root
     */
    protected $_root;

    /**
     * Constructor
     *
     * Initialize environment, root path, and configuration.
     *
     * @param  string $env
     * @param  string|null $root
     * @return void
     */
    public function __construct($env, $root = null)
    {
        $this->_setEnv($env);
        if (null === $root) {
            $root = realpath(dirname(__FILE__) . '/../');
        }
        $this->_root = $root;

        $this->initConfig();

        $this->_front = Zend_Controller_Front::getInstance();

        // set the test environment parameters
        if ($env == 'test') {
            // Enable all errors so we'll know when something goes wrong.
            error_reporting(E_ALL | E_STRICT);
            ini_set('display_startup_errors', 1);
            ini_set('display_errors', 1);

            $this->_front->throwExceptions(true);
        }
    }

    /**
     * Initialize environment
     *
     * @param  string $env
     * @return void
     */
    protected function _setEnv($env)
    {
        $this->_env = $env;
    }

    /**
     * Route startup
     *
     * @return void
     */
    public function routeStartup(Zend_Controller_Request_Abstract $request)
    {
        $this->initDb()
            ->initHelpers()
            ->initView()
            ->initPlugins()
            ->initRoutes()
            ->initControllers()
            ->initLog()
            ->initPubSub();

    }

    /**
     * Initialize Data bases
     *
     * @return Smithy_Plugin_Initialize
     */
    public function initConfig ()
    {
        self::$_config = new Zend_Config_Ini($this->_root . '/application/config/app.ini', $this->_env);
        Zend_Registry::set('config', self::$_config);

        return $this;
    }

    /**
     * Initialize data bases
     *
     * @return Smithy_Plugin_Initialize
     */
    public function initDb()
    {
        Zend_Registry::set('db', Zend_Db::factory(Zend_Registry::get('config')->database->adapter, Zend_Registry::get('config')->database->params));
        Zend_Db_Table_Abstract::setDefaultAdapter(Zend_Registry::get('db'));
        Zend_Db_Table_Abstract::setDefaultMetadataCache();

        return $this;
    }

    /**
     * Initialize action helpers
     *
     * @return Smithy_Plugin_Initialize
     */
    public function initHelpers()
    {
        // register the default action helpers
        Zend_Controller_Action_HelperBroker::addPath($this->_root . '/application/default/helpers', 'Zend_Controller_Action_Helper');

        return $this;
    }

    /**
     * Initialize view
     *
     * @return Smithy_Plugin_Initialize
     */
    public function initView()
    {
        // Bootstrap layouts
        Zend_Layout::startMvc(array(
            'layoutPath' => $this->_root . '/application/default/layouts',
            'layout' => 'main'
        ));

        return $this;
    }

    /**
     * Initialize plugins
     *
     * @return Smithy_Plugin_Initialize
     */
    public function initPlugins()
    {
        return $this;
    }

    /**
     * Initialize logging
     *
     * @return Smithy_Plugin_Initialize
     */
    public function initLog()
    {
        $this->_log = new Zend_Log(new Zend_Log_Writer_Stream($this->_root . '/log/application.log'));
        $this->_log->info('=============================================================================================');

        return $this;
    }

    /**
     * Initialize routes
     *
     * @return Smithy_Plugin_Initialize
     */
    public function initRoutes()
    {
        $routeConfig = new Zend_Config_Ini($this->_root . '/application/config/routes.ini', $this->_env);
        Zend_Registry::set('routeConfig', $routeConfig);

        $router = $this->_front->getRouter();
        $router->addConfig($routeConfig, 'routes');

        return $this;
    }

    /**
     * Initialize Controller paths
     *
     * @return Smithy_Plugin_Initialize
     */
    public function initControllers()
    {
        $this->_front->addControllerDirectory($this->_root . '/application/default/controllers', 'default');

        return $this;
    }

    /**
     * Initialize subscriptions to Phly events
     *
     * @return Smithy_Plugin_Initialize
     */
    public function initPubSub()
    {
        Phly_PubSub::subscribe('log', $this->_log, 'info');

        return $this;
    }


}
